package com.cloudcc.mobile.dialog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.util.datetime.NumericWheelAdapter;
import com.cloudcc.mobile.util.datetime.OnWheelScrollListener;
import com.cloudcc.mobile.util.datetime.WheelView;

public class DatetimePickDialog {
	private LayoutInflater inflater = null;
	private Activity activity;
	private String title;
	private TextView datetime;
	private TextView showtime;
	private AlertDialog dialog;
	private WheelView year;
	private WheelView month;
	private WheelView day;
	private WheelView hour;
	private WheelView mins;
	public DatetimePickDialog(Activity activity, String title, TextView datetime ,AlertDialog dialog,LayoutInflater inflater) {
		this.activity = activity;
		this.title = title;
		this.datetime = datetime;
		this.dialog = dialog;
		this.inflater = inflater;
	}
	public AlertDialog ShowDatetimeDialog(){
		
		dialog = new AlertDialog.Builder(activity).setTitle(title).setView(getDateTimePick()).show();
		return dialog;
		
	}
	private View getDateTimePick(){
		Calendar c = Calendar.getInstance();
		int curYear = c.get(Calendar.YEAR);
		int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
		int curDate = c.get(Calendar.DATE);
		int curhour = c.get(Calendar.HOUR_OF_DAY);
		int curmins = c.get(Calendar.MINUTE);
		final View view = inflater.inflate(R.layout.datatimepick, null);
		showtime = (TextView) view.findViewById(R.id.show_datetime);
		year = (WheelView) view.findViewById(R.id.datatime_year);
		year.setAdapter(new NumericWheelAdapter(1950, curYear+20));
		year.setLabel("年");
		year.setCyclic(true);
		year.addScrollingListener(scrollListener);
		month = (WheelView) view.findViewById(R.id.datatime_month);
		month.setAdapter(new NumericWheelAdapter(1, 12,"%02d"));
		month.setLabel("月");
		month.setCyclic(true);
		month.addScrollingListener(scrollListener);
		
		day = (WheelView) view.findViewById(R.id.datatime_day);
		initDay(curYear,curMonth);
		day.setLabel("号");
		day.setCyclic(true);
		day.addScrollingListener(scrollListener);

		year.setCurrentItem(curYear - 1950);
		month.setCurrentItem(curMonth - 1);
		day.setCurrentItem(curDate - 1);
		hour = (WheelView) view.findViewById(R.id.datatime_hour);
		hour.setAdapter(new NumericWheelAdapter(0, 23,"%02d"));
		hour.setLabel("时");
		hour.setCyclic(true);
		mins = (WheelView) view.findViewById(R.id.datatime_mins);
		mins.setAdapter(new NumericWheelAdapter(0, 59,"%02d"));
		mins.setLabel("分");
		mins.setCyclic(true);
		
		hour.setCurrentItem(curhour);
		mins.setCurrentItem(curmins);
		
		Button bt = (Button) view.findViewById(R.id.datatime_set);
		bt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String strdatetime = (year.getCurrentItem()+1950) + "-"+ zero(month.getCurrentItem()+1)+"-"+
						zero(day.getCurrentItem()+1)+" "+zero(hour.getCurrentItem()) + ":"+ zero(mins.getCurrentItem());
				datetime.setText(strdatetime);
				dialog.dismiss();
			}
		});
		Button cancel = (Button) view.findViewById(R.id.datatime_cancel);
		cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				menuWindow.dismiss();
				dialog.dismiss();
			}
		});
		return view;
	}
	
	OnWheelScrollListener scrollListener = new OnWheelScrollListener() {

		@Override
		public void onScrollingStarted(WheelView wheel) {

		}

		@Override
		public void onScrollingFinished(WheelView wheel) {
			// TODO Auto-generated method stub
			int n_year = year.getCurrentItem() + 1950;// 楠烇拷
			int n_month = month.getCurrentItem() + 1;// 閺堬拷
			initDay(n_year,n_month);
			String datetime1 = (year.getCurrentItem()+1950) + "-"+ zero(month.getCurrentItem()+1)+"-"+
					zero(day.getCurrentItem()+1)+" "+zero(hour.getCurrentItem()) + ":"+ zero(mins.getCurrentItem());
//			showtime.setText(datetime1);
		}
	};
	
	private void initDay(int arg1, int arg2) {
		day.setAdapter(new NumericWheelAdapter(1, getDay(arg1, arg2), "%02d"));
		
	}
	
	private int getDay(int year, int month) {
		int day = 30;
		boolean flag = false;
		switch (year % 4) {
		case 0:
			flag = true;
			break;
		default:
			flag = false;
			break;
		}
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			day = 31;
			break;
		case 2:
			day = flag ? 29 : 28;
			break;
		default:
			day = 30;
			break;
		}
		return day;
	}
	private String getWeek(String Week, SimpleDateFormat formatter, String cTime) {
		Calendar c = Calendar.getInstance();
		Week = "星期";
		try {
			c.setTime(formatter.parse(cTime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		switch (c.get(Calendar.DAY_OF_WEEK)) {
		case 1:
			Week += "天";
			break;
		case 2:
			Week += "一";
			break;
		case 3:
			Week += "二";
			break;
		case 4:
			Week += "三";
			break;
		case 5:
			Week += "四";
			break;
		case 6:
			Week += "五";
			break;
		case 7:
			Week += "六";
			break;
		default:
			break;
		}
		return Week;
	}
	public String zero(int digital){
		if(digital<10){
			return "0"+digital;
		}else{
			return ""+digital;
		}
		
	}
}
